package com.app.songbump;

import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ListActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class SongBumpFriendsActivity extends ListActivity {

	AppPreferences appPrefs;
	private ArrayList<Friend> friends;
	Intent intent;
	String uid;
	private Button addButton;

	private GetFriendsReceiver receiver;
	
	public class GetFriendsReceiver extends BroadcastReceiver {
	   @Override
	    public void onReceive(Context context, Intent intent) {
		   	String response = intent.getStringExtra("response");
		   
			friends = new ArrayList<Friend>();
			try {

				JSONArray jsonArray = new JSONArray(response);
				for (int index = 0; index < jsonArray.length(); index++) {
					JSONObject jsonObject = jsonArray.getJSONObject(index);
					String name = jsonObject.getString("name");
					String id = jsonObject.getString("id");
					String ip = jsonObject.getString("ip");
					String playing = jsonObject.getString("playing");
					if (playing.equals("1"))
						name += " - Online Now";
					friends.add(new Friend(name, id, ip));
				}

			} catch (JSONException e) {
				e.printStackTrace();
			}
			
			ArrayList<String> names = new ArrayList<String>();
			for (int i = 0; i < friends.size(); i++)
				names.add(friends.get(i).name);
			ArrayAdapter<String> friendList = new ArrayAdapter<String>(
					getApplicationContext(), R.layout.list_item, names);
			setListAdapter(friendList);
		}
	}
	
	public class Friend {
		public String name;
		public String id;
		public String ip;
		public Friend(String name, String id, String ip) {
			this.name = name;
			this.id = id;
			this.ip = ip;
		}
	}

	public void onDestroy() {
		super.onDestroy();
		unregisterReceiver(receiver);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.friendlist);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		appPrefs = new AppPreferences(getApplicationContext());
		uid = appPrefs.getUserId();
		
		IntentFilter filter = new IntentFilter(SongBumpServer.GET_FRIENDS_RESPONSE);
		filter.addCategory(Intent.CATEGORY_DEFAULT);
		receiver = new GetFriendsReceiver();
		registerReceiver(receiver, filter);
		
		Intent msgIntent = new Intent(SongBumpFriendsActivity.this, SongBumpServer.class);
		msgIntent.putExtra(SongBumpServer.ACTION,SongBumpServer.GET_FRIENDS);
		msgIntent.putExtra("id",uid);
		startService(msgIntent);

		addButton = (Button) this.findViewById(R.id.addFriend);
		addButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				intent = new Intent(getApplicationContext(), BumpActivity.class);
				startActivity(intent);
				SongBumpFriendsActivity.this.onPause();
			}

		});
	}

	protected void onResume() {
		super.onResume();
		Intent msgIntent = new Intent(SongBumpFriendsActivity.this, SongBumpServer.class);
		msgIntent.putExtra(SongBumpServer.ACTION,SongBumpServer.GET_FRIENDS);
		msgIntent.putExtra("id",uid);
		startService(msgIntent);
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		
		Bundle b = new Bundle();
		intent = new Intent(getApplicationContext(),
				SongBumpFriendInfoActivity.class);
		b.putString("name", friends.get(position).name);
		b.putString("id", friends.get(position).id);
		b.putString("ip", friends.get(position).ip);
		intent.putExtras(b);
		startActivity(intent);
		setResult(100, intent);
		this.onPause();
		
	}
}
